clear all

*Set working folders. Please change the global variable "user" to the folder with the replication package
global user "C:\Users\weilingliu1990\Dropbox\Insurance Project\LTCI Replication Package"
global data "$user\Data"
global images "$user\Images"
global drafttab "$user\Tables"

set maxvar  32767
*ssc install estout						//will need to install if you don't already have it



//TABLE 1A
use "$data\matched_sample_data.dta", clear
replace req_rate=req_rate*100
replace appr_rate=appr_rate*100
estpost tabstat req_rate appr_rate remainder approval2 approval delay, columns(statistics) statistics(mean sd min p25 p50 p75 max n) 
esttab using "$drafttab\Table_1A.tex", title(Summary Statistics for Rate Increase Applications) cells("mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) p50(fmt(2)) p25(fmt(%9.2fc)) p75(fmt(%9.2fc)) max(fmt(%9.2fc)) count(fmt(%9.0fc))") noobs nomtitle nonumber label replace
eststo clear


//TABLE 2: Examine requests submitted to 1 vs multiple states
use "$data\matched_sample_data.dta", clear
**** PART A: 1 state only
estpost tabstat req_rate approval2 approval remainder avg_prem inforce_ct mkt_shr if single==1, columns(statistics) statistics(mean sd min p25 p50 p75 max n) 
esttab using "$drafttab\Table_2A.tex", title(Summary Statistics for Rate Increase Applications) cells("mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) p50(fmt(2)) p25(fmt(%9.2fc)) p75(fmt(%9.2fc)) max(fmt(%9.2fc)) count(fmt(%9.0fc))") noobs nomtitle nonumber label replace
eststo clear
**** PART B: multiple states
estpost tabstat req_rate approval2 approval remainder avg_prem inforce_ct mkt_shr if single==0, columns(statistics) statistics(mean sd min p25 p50 p75 max n) 
esttab using "$drafttab\Table_2B.tex", title(Summary Statistics for Rate Increase Applications) cells("mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) p50(fmt(2)) p25(fmt(%9.2fc)) p75(fmt(%9.2fc)) max(fmt(%9.2fc)) count(fmt(%9.0fc))") noobs nomtitle nonumber label replace
eststo clear
**** PART C: test their differences
eststo: reg req_rate i.single
eststo: reg remainder i.single 
eststo: reg approval2 i.single 
eststo: reg approval i.single 
eststo: reg avg_prem i.single 
eststo: reg inforce_ct i.single 
eststo: reg  mkt_shr i.single 
esttab using "$drafttab\Table_2C.tex" , b(a2) se(a2) star(* 0.10 ** 0.05 *** 0.01) ///
sfmt(%4.2f %4.2f %4.2f %7.0fc %4.2f %4.3f) noobs lines ///
keep(*single*) nogaps numbers nobaselevels f replace
eststo clear
 

 
//TABLE 3: Examine requests by how close they were to election year (only for those submitted to multiple states)
**** PART A: 1 year before election
use "$data\matched_sample_data.dta", clear
estpost tabstat req_rate  remainder  approval2 approval avg_prem inforce_ct mkt_shr if yrs_left==1 & check_states>1, columns(statistics) statistics(mean sd min p25 p50 p75 max n) 
esttab using "$drafttab\Table_3A.tex", title(Summary Statistics for Rate Increase Applications) cells("mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) p50(fmt(2)) p25(fmt(%9.2fc)) p75(fmt(%9.2fc)) max(fmt(%9.2fc)) count(fmt(%9.0fc))") noobs nomtitle nonumber label replace
eststo clear
**** PART B: all other years
estpost tabstat req_rate  remainder  approval2 approval avg_prem inforce_ct mkt_shr if yrs_left>1 & check_states>1, columns(statistics) statistics(mean sd min p25 p50 p75 max n) 
esttab using "$drafttab\Table_3B.tex", title(Summary Statistics for Rate Increase Applications) cells("mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) p50(fmt(2)) p25(fmt(%9.2fc)) p75(fmt(%9.2fc)) max(fmt(%9.2fc)) count(fmt(%9.0fc))") noobs nomtitle nonumber label replace
eststo clear
**** PART C: test their differences
gen election=yrs_left==1
eststo: reg req_rate i.election if check_states>1
eststo: reg remainder i.election if check_states>1
eststo: reg approval2 i.election if check_states>1
eststo: reg approval i.election if check_states>1
eststo: reg avg_prem i.election if check_states>1
eststo: reg inforce_ct i.election if check_states>1
eststo: reg mkt_shr i.election if check_states>1
esttab using "$drafttab\Table_3C.tex" , b(a2) se(a2) star(* 0.10 ** 0.05 *** 0.01) ///
sfmt(%4.2f %4.2f %4.2f %7.0fc %4.2f %4.3f) noobs lines ///
keep(*election*) nogaps numbers nobaselevels f replace
eststo clear


//FIGURE 3		
/* notes: this main dataset is structured so that yrs_left is based off of the requested date. 
in the code below, we calculate the difference between requested year and accepted year, and we adjust the yrs_left accordingly.
After that, we can summarize both requests and acceptances by yrs left before election
*/
use "$data\matched_sample_data.dta", clear
drop if yrs_left==. | term_length!="4"
gen flag=approval2==0 & approval==100	& dateappr!=.							//flag those that were approved with a delay
expand 2 if flag==1, gen(flag2)													//duplicate these observations, with flag2=1 if they're a dupe
replace req_rate=. if flag2==1													//remove the other measures (eg rates, counts) from duplicates
replace approval=. if flag2==0 & flag==1										//now, for non-duplicates, we remove the approved measure
gen gap=year(dateappr)-year														//measure the # years of delay
replace gap=1 if gap==-1799														//fix typo		
replace gap=0 if gap==-1800														//fix typo
replace yrs_left=yrs_left-gap if flag2==1 & flag==1								//we need to adjust the yrs_left for those duplicate years
replace yrs_left=4 if inlist(yrs_left,0,-4,8)			
replace yrs_left=3 if inlist(yrs_left,-1,-5,7)
replace yrs_left=2 if inlist(yrs_left,-2,-6,6)
replace yrs_left=1 if inlist(yrs_left,-3,5,9)
replace approval=approval/100													//change to a count of 1 if approved
collapse (count) ct_req= req_rate (sum) approval, by(yrs_left)					//plotting these as bar graphs gives Figure 3


use "$data\matched_sample_data.dta", clear
drop if check_states==1
label variable approval2  "Approval in Same Yr"
label variable approval  "Approval"
label variable remainder  "% Increase Granted"
label variable num_approved "Amt Prev Approved"

drop if yrs_left==0
egen double_cluster=group(year state_num)
replace approval=0 if approval==.

//FIGURE 4A 
binscatter approval2 yrs_left, controls(i.state_num) absorb(request_id3) ytitle("Probability of Approval (%)") xtitle("Years Left in Term") xsc(reverse) ///
lcolor(gray) ylab(, nogrid) graphregion(color(white)) legend(off) yscale(range(35 65)) xscale(range(.5 4.5)) ytick(35(10)65) ylabel(#10)
graph export "$images\Figure_4A.png", replace
 
 
************************* Regressions *******************************
//TABLE 4
eststo:areg approval2 yrs_left num_approved amt_approved i.state_num , absorb(request_id2)  vce(cluster request_id2)
quietly summ approval2 
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo:areg approval2 yrs_left num_approved amt_approved i.state_num , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo:areg approval yrs_left num_approved amt_approved i.state_num , absorb(request_id2)  vce(cluster request_id2)
quietly summ approval 
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo:areg approval yrs_left num_approved amt_approved i.state_num , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval 
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo: areg remainder yrs_left num_approved amt_approved i.state_num , absorb(request_id2) vce(cluster request_id2)
quietly summ remainder 
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo: areg remainder yrs_left num_approved amt_approved i.state_num , absorb(request_id3) vce(cluster request_id3)
quietly summ remainder
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

esttab using "$drafttab\Table_4.tex" , b(a2) se(a2) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("ysum Mean Dependent Variable" "stfe State FE" "amtfe Policy x Year x Amt FE" "N Number of Observations" "r2 R-squared") ///
sfmt(%4.2f %4.2f %4.2f %7.0fc %4.2f %4.3f) noobs lines ///
keep(yrs_left_in_term num_approved amt_approved) varlabel(_cons "Constant" yrs_left_in_term "Years Left in Term") nogaps numbers nobaselevels f replace
eststo clear
 
 
//TABLE 7
gen democrat= (political_party=="D" | political_party==".D")
label var democrat "Democrat"

sort state year
merge m:1 state year using "$data\commissioner_data"							//merge in detailed commissioner data
drop if _merge==2																//not merged are just the extra years
drop _merge

gen chg_rep= chg_in_party==1
gen chg_dem= chg_in_party==-1
gen close_race_dem= chg_in_party==-1 & margin<10
gen close_race_rep= chg_in_party==1 & margin<10
gen close_race=margin<10

eststo:areg approval2 democrat num_approved amt_approved  i.state_num , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo:areg approval2 chg_dem chg_rep num_approved amt_approved i.state_num , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
 
eststo:areg approval2  close_race  close_race_rep num_approved amt_approved i.state_num , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo:areg remainder democrat num_approved amt_approved i.state_num , absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo:areg remainder chg_dem chg_rep num_approved amt_approved i.state_num , absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo:areg remainder close_race  close_race_rep num_approved amt_approved i.state_num , absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

esttab using "$drafttab\Table_7.tex" , b(a2) se(a2) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("ysum Mean Dependent Variable" "stfe State FE" "amtfe Policy x Year x Amt FE" "N Number of Observations" "r2 R-squared") ///
sfmt(%4.2f %4.2f %4.2f %7.0fc %4.2f %4.3f) noobs lines ///
keep(close_race* chg_* democrat num_approved amt_approved ) order(democrat chg_* close_race* num_approved amt_approved)  varlabel(_cons "Constant" yrs_left_in_term "Years Left in Term") nogaps numbers nobaselevels f replace
eststo clear
 

//TABLE 5
encode name, gen(name_num)
replace margin=. if margin>99													//remove those that don't make sense
replace winpct=. if winpct>99													//remove those that don't make sense
eststo: areg approval2 c.yrs_left_in_term c.margin i.state_num i.year num_approved amt_approved ,  absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg approval2 c.yrs_left_in_term##c.margin i.state_num i.year num_approved amt_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg approval2 c.yrs_left_in_term##c.margin i.name_num i.year num_approved amt_approved ,  absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo: areg remainder c.yrs_left_in_term c.margin i.state_num i.year  num_approved amt_approved ,  absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg remainder c.yrs_left_in_term##c.margin i.state_num i.year num_approved amt_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg remainder c.yrs_left_in_term##c.margin i.name_num i.year num_approved amt_approved ,  absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

esttab using "$drafttab\Table_5.tex" , b(%4.2f) se(%4.2f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("ysum Mean Dependent Variable" "stfe State FE" "amtfe Policy x Year x Amt FE" "N Number of Observations" "r2 R-squared") ///
sfmt(%4.2f %4.2f %4.2f %7.0fc %4.2f %4.3f) noobs lines ///
keep(yrs_left_in_term margin c.yrs_left_in_term#c.margin num_approved amt_approved) nogaps numbers nobaselevels f replace
eststo clear
 
//TABLE 6
eststo: areg approval2 c.yrs_left_in_term c.tenure i.state_num i.year num_approved amt_approved ,  absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg approval2 c.yrs_left_in_term##c.tenure i.state_num i.year num_approved amt_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg approval2 c.yrs_left_in_term##c.tenure i.name_num i.year num_approved amt_approved ,  absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo: areg remainder c.yrs_left_in_term c.tenure i.state_num i.year  num_approved amt_approved ,  absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg remainder c.yrs_left_in_term##c.tenure i.state_num i.year num_approved amt_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg remainder c.yrs_left_in_term##c.tenure i.name_num i.year num_approved amt_approved ,  absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if winpct<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
esttab using "$drafttab\Table_6.tex" , b(%4.2f) se(%4.2f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("ysum Mean Dependent Variable" "stfe State FE" "amtfe Policy x Year x Amt FE" "N Number of Observations" "r2 R-squared") ///
sfmt(%4.2f %4.2f %4.2f %7.0fc %4.2f %4.3f) noobs lines ///
keep(yrs_left_in_term tenure c.yrs_left_in_term#c.tenure num_approved amt_approved) nogaps numbers nobaselevels f replace
eststo clear
 

//TABLE 8
merge m:1 state year using "$data\campaign_finance_data.dta"					//merge in the campaign finance dataset
drop if _merge==2 																//non-elected states are not matched, and thus, dropped
drop _merge

replace contributions = contributions/100000									//convert to units of 100K 
replace cash = cash/100000														//convert to units of 100K 

eststo: areg approval2 c.yrs_left_in_term c.cash i.co_code i.year i.state_num amt_approved num_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if cash<. & cont<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg approval2 c.yrs_left_in_term c.cont  i.co_code i.year i.state_num amt_approved num_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if cash<. & cont<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg  approval2 c.yrs_left_in_term c.cont c.cash i.co_code i.year i.state_num amt_approved num_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ approval2 if cash<. & cont<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

eststo: areg remainder c.yrs_left_in_term  c.cash i.co_code i.year i.state_num amt_approved num_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if cash<. & cont<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg remainder c.yrs_left_in_term c.cont  i.co_code i.year i.state_num amt_approved num_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if cash<. & cont<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"
eststo: areg remainder c.yrs_left_in_term c.cont c.cash i.co_code i.year i.state_num amt_approved num_approved , absorb(request_id3)  vce(cluster request_id3)
quietly summ remainder if cash<. & cont<.
estadd scalar ysum = r(mean)
estadd local stfe "Yes"
estadd local amtfe "Yes"

esttab using "$drafttab\Table_8.tex" , b(%4.2f) se(%4.2f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("ysum Mean Dependent Variable" "stfe State FE" "amtfe Policy x Year x Amt FE" "N Number of Observations" "r2 R-squared") ///
sfmt(%4.2f %4.2f %4.2f %7.0fc %4.2f %4.3f) noobs lines ///
 keep(yrs_left_in_term cashonhand contributions num_approved amt_approved)  order (yrs_left_in_term cashonhand contributions num_approved amt_approved)  ///
 varlabel(cashonhand "Prev Contributions Raised before Year Start" contributions "New Contributions Raised over the Year") nogaps numbers nobaselevels f ///
replace
eststo clear
